package com.service.impl.user;

import com.core.dto.PageDTO;
import com.core.dto.UserDTO;
import com.core.model.user.Administrator;
import com.core.model.user.User;
import com.core.model.user.UserInfo;
import com.dao.AdminDao;
import com.dao.UserDao;
import com.service.user.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-10-23
 * Time: 下午2:52
 * To change this template use File | Settings | File Templates.
 */
@Service("administratorService")
@Transactional
public class AdministratorServiceImpl implements AdministratorService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User admin = userDao.findAdministratorByUsername(username);
        return admin;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Map<String, Object> findAllAdminUser(String currentPage) {
        Map<String, Object> map = new HashMap<>();
        List<User> users = adminDao.findAllAdminUser(currentPage);
        List<UserDTO> userDTOs = UserDTO.toDTOs(users);
        String totalPage = adminDao.findTotalPage(Administrator.class).toString();
        currentPage = PageDTO.isPage(currentPage, totalPage);
        map.put("userDTOs", userDTOs);
        map.put("currentPage", currentPage);
        map.put("totalPage", totalPage);
        return map;
    }

    @Override
    @Transactional
    public Map<String, Object> addAdminUser(UserDTO userDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            Administrator find = adminDao.findAdminByUsername(userDTO.getUsername());
            if (find == null) {
                Administrator administrator = new Administrator(userDTO.getUsername(), passwordEncoder.encode(userDTO.getPassword()));
                UserInfo userInfo = new UserInfo(administrator, userDTO.getNickname());
                administrator.initUserInfo(userInfo);
                adminDao.saveOrUpdateAdmin(administrator);
                map.put("result", true);
                map.put("data", UserDTO.toDTO(administrator));
            } else {
                map.put("result", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        }
        return map;
    }

    @Override
    @Transactional
    public boolean delAdminUser(String adminUserGuid) {
        boolean result = false;
        try {
            Administrator administrator = adminDao.findByGuid(Administrator.class, adminUserGuid);
            adminDao.deleteAdmin(administrator);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    @Transactional
    public boolean resetAdminPwd(String adminUserGuid, String newPassword) {
        boolean result = false;
        try {
            Administrator administrator = adminDao.findByGuid(Administrator.class, adminUserGuid);
            administrator.setPassword(passwordEncoder.encode(newPassword));
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Object findTotalPage(Class c) {
        return adminDao.findTotalPage(c);
    }

    @Override
    public Administrator findAdminByUsername(String username) {
        return adminDao.findAdminByUsername(username);
    }


}
